package com.dlut.java4;

/**
 * @Author: LiuHeng
 * @Description:
 * @Date: 2022-09-22 10:58
 */
public class MyMap {
    int length = 16;
    Entry[] arr = new Entry[length];
    int size;
    public MyMap(){
        size  = 0;
    }

    public boolean delete(int k){
        int i = k % length;
        if (arr[i] == null){
            return false;
        }
        Entry temp = arr[i];
        if (temp.k == k){
            arr[i] = temp.next;
            return true;
        }
        Entry front = arr[i];
        temp = temp.next;
        while (temp != null){
            if (temp.k == k){
                front.next = temp.next;
                return true;
            }
            front = temp;
            temp = temp.next;
        }

        return false;
    }

    public void put(int k, String value){

        int i = k % length;
        if (arr[i] == null){
            arr[i] = new Entry(k, value);
        }else {
            Entry temp = arr[i];

            while (temp != null){
                if (temp.k == k){
                    temp.value = value;
                    return;
                }
                temp = temp.next;
            }

            Entry entry = new Entry(k, value);
            entry.next = arr[i];
            arr[i] = entry;
        }

    }

    public String get(int k){
        int i = k % 16;
        if (arr[i] == null){
            return null;
        }else {
            Entry temp = arr[i];
            while (temp != null){
                if (temp.k == k){
                    return temp.value;
                }
                temp = temp.next;
            }
        }
        return null;
    }



    class Entry{
        int k;
        String value;
        Entry next;
        public Entry(){

        }

        public Entry(int k, String value) {
            this.k = k;
            this.value = value;
        }
    }
}



